import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by us on 2016/10/24.
 */
public class Zoom_2 {
    private int destW ;
    private int destH ;

    public Zoom_2() {

    }


    public static int interpolation(int[] imageData, int width, int height, float x, float y) {
        // 四个最临近象素的坐标(i1, j1), (i2, j1), (i1, j2), (i2, j2)
        int i1, i2;
        int j1, j2;

        int f1, f2, f3, f4; // 四个最临近象素值
        int f12, f34; // 二个插值中间值

        //f1 = f2 = f3 = f4 = 0;

        // 定义一个值，当象素坐标相差小于改值时认为坐标相同
        int EXP = 0;

        // 计算四个最临近象素的坐标
        i1 = (int) x;
        i2 = i1 + 1;
        j1 = (int) y;
        j2 = j1 + 1;

        // 根据不同情况分别处理
        if ((x < 0) || (x > width - 1) || (y < 0) || (y > height - 1)) {
            return 0x00ffffff; // 要计算的点不在源图范围内，直接返回255。
        } else {
            if (Math.abs(x - width + 1) <= EXP) {
                // 要计算的点在图像右边缘上
                if (Math.abs(y - height + 1) <= EXP) {
                    // 要计算的点正好是图像最右下角那一个象素，直接返回该点象素值
                    f1 = (int) imageData[width * j1 + i1];
                    return (int) f1;
                } else {
                    // 在图像右边缘上且不是最后一点，直接一次插值即可
                    f1 = (int) imageData[width * j1 + i1];
                    f3 = (int) imageData[width * j1 + i2];

                    // 返回插值结果
                    return (int) (f1 + (y - j1) * (f3 - f1));
                }
            } else if (Math.abs(y - height + 1) <= EXP) {
                // 要计算的点在图像下边缘上且不是最后一点，直接一次插值即可
                f1 = (int) imageData[width * j1 + i1];
                f2 = (int) imageData[width * j2 + i1];

                // 返回插值结果
                return (int) (f1 + (x - i1) * (f2 - f1));
            } else {
                // 计算四个最临近象素值
                f1 = imageData[width * j1 + i1];
                f2 = imageData[width * j1 + i2];
                f3 = imageData[width * j2 + i1];
                f4 = imageData[width * j2 + i2];


               //插值1
                f12 = (int) (f1 + (x - i1) * (f2 - f1));
                // 插值2
                f34 = (int) ((f3 + (x - i1) * (f4 - f3)));

                return (int) (f12 + (y - j1) * (f34 - f12));
            }
        }

    }
    public void newPicture ()throws IOException{
        try {
            File file = new File("data/pikaqiu.jpg");
            BufferedImage src = ImageIO.read(file);
            BufferedImage dest = new BufferedImage(600,600, BufferedImage.TYPE_3BYTE_BGR);
            int destW = 600; //缩放后图片的宽度
            int destH = 600; //缩放图片的高度
            File outfile = new File("P11.jpg");
            int srcW = src.getWidth();
            int srcH = src.getHeight();
            // create pixel arrays
            int[] destPixels = new int[destW * destH]; // array to hold destination
            int[] destRed = new int[destW * destH];
            int[] destGreen = new int[destW * destH];
            int[] destBlue = new int[destW * destH];
            // pixels
            int[] srcPixels = new int[srcW * srcH]; // array with source's pixels
            int[] srcRed = new int[srcW * srcH];
            int[] srcGreen = new int[srcW * srcH];
            int[] srcBlue = new int[srcW * srcH];
            src.getRGB( 0, 0, srcW, srcH,srcPixels,0,srcW);

            for (int i = 0; i < srcPixels.length; i++) {
                srcBlue[i] = srcPixels[i]&0x000000ff;
                srcGreen[i] = (srcPixels[i]>>8)&0x000000ff;
                srcRed[i] = (srcPixels[i]>>16)&0x000000ff;
            }
            // simple point smapled resizing
            // loop through the destination pixels, find the matching pixel on
            // the source and use that
            for (int destY = 0; destY < destH; ++destY) {
                for (int destX = 0; destX < destW; ++destX) {
                    float srcX = ((float)destX * (float)srcW) / (float)destW;
                    float srcY = ((float)destY * (float)srcH) / (float)destH;
                    try {
                        destRed[destX + destY * destW] = Zoom_2.interpolation(srcRed, srcW, srcH, srcX, srcY);
                        destGreen[destX + destY * destW] = Zoom_2.interpolation(srcGreen, srcW, srcH, srcX, srcY);
                        destBlue[destX + destY * destW] = Zoom_2.interpolation(srcBlue, srcW, srcH, srcX, srcY);

                        int value = 0xff000000;
                        value|=destBlue[destX + destY * destW];
                        value|=(destGreen[destX + destY * destW]<<8);
                        value|=(destRed[destX + destY * destW]<<16);

                        destPixels[destX + destY * destW] = value;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            dest.setRGB(0,0,destW,destH,destPixels,0,destW);
            ImageIO.write(dest,"jpg",outfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}






